package controller;

import entity.hotel_employee;
import service.HotelEmployeeService;
import serviceImp.HotelEmployeeServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Servlet implementation class EmployeeInfoController
 */
@WebServlet("/employee/EmployeeChangeInfoController.do")
public class EmployeeChangeInfoController extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public EmployeeChangeInfoController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");

		String employee_name = request.getParameter("employee_name");
		String employee_sex = request.getParameter("employee_sex");
		String password = request.getParameter("password");
		String rePassword = request.getParameter("rePassword");

		if(password==null||rePassword==null||!password.equals(rePassword)){
			request.setAttribute("passwordTip","密码重设失败，请重试");
			request.getRequestDispatcher("/employee/myInfo.jsp").forward(request,response);
		}
		HttpSession session = request.getSession();
		HotelEmployeeService hotelEmployeeService = new HotelEmployeeServiceImp();
		hotel_employee hotel_employee = new hotel_employee(employee_name,employee_sex,password
				,(String)session.getAttribute("loginTel"));
		int code = hotelEmployeeService.employeeChangeInfo(hotel_employee);
		if(code==0){
			request.setAttribute("fileTip","信息修改失败，请重试");
			request.getRequestDispatcher("/employee/myInfo.jsp").forward(request,response);
		}
		session.invalidate();
		request.setAttribute("successTip","信息修改成功，请刷新后重新登录");
		request.getRequestDispatcher("../employee/myInfo.jsp").forward(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
